<template>
	<view class="myAddress">
		<view class="addressbox" v-for="(item,index) in addressList" key="index">
			<view class="leftbox" @click="goBack(item)">
				<view class="address">
					<text class="site">{{item.province}}{{item.city}}{{item.area}}{{item.detailedAddress}}</text>
					<text class="default" v-if="item.isDefaultAddress">默认</text>
					<!-- <text class="home">家</text> -->

				</view>
				<view class="name">
					{{item.realname}} {{item.telephone}}
				</view>

			</view>
			<view class="rightbox" @click="modifyFun(item)">
				<uni-icons type="compose" :size="24" color="#BDBCC1"></uni-icons>
			</view>
		</view>

		<view class="addNew" @click="newAddressFun">
			新增地址
		</view>
	</view>
</template>

<script setup>
	const {
		proxy
	} = getCurrentInstance()
	const newAddressFun = () => {
		proxy.sjuNav.navigateTo('/my/addNewAddress')
	}
	const openId = ref(null)
	const addressList = ref(null)
	const queryAddressListFun = () => {
		proxy.sjuRequest.request("/api/weChatMall/address/queryAddressList/" + proxy.sjuLogin.getValue("miaoMaGJOpenid"), 'post').then(res => {
			console.log(res)
			addressList.value = res.data
			console.log(addressList.value)
		})
	}
	const modifyFun = (item) => {
		console.log(item.id)
		proxy.sjuNav.navigateTo('/my/modifyAddress?address=' + item.id)
	}
	onShow(() => {
		console.log('shaxin')
		queryAddressListFun()
	})


	const goBack = (item) => {
		let pages = getCurrentPages() // 获取栈实例
		let page = pages[pages.length - 1] // 获取当前页面的数据，包含页面路由
		let prevPage = pages[pages.length - 2] // 获取上个页面的数据，包含页面
		console.log(item)
		
		
		if(prevPage.route=='sub_my/pages/sureOrder/sureOrder'){
			
			proxy.sjuRequest.request("/api/weChatMall/address/queryAddressList/" + proxy.sjuLogin.getValue("miaoMaGJOpenid"), 'post').then(res => {
				console.log(res.data)
				for(let i=0;i<res.data.length;i++){
					if(res.data[i].id==item.id){					
						proxy.sjuLogin.saveValue("realname",res.data[i].realname)
						proxy.sjuLogin.saveValue("telephone",res.data[i].telephone)
						proxy.sjuLogin.saveValue("province",res.data[i].province)
						proxy.sjuLogin.saveValue("city",res.data[i].city)
						proxy.sjuLogin.saveValue("area",res.data[i].area)
						proxy.sjuLogin.saveValue("detailedAddress",res.data[i].detailedAddress)
						proxy.sjuLogin.saveValue("isDefaultAddress",res.data[i].isDefaultAddress)
						proxy.sjuLogin.saveValue("id",res.data[i].id)
					}
					
				}
				
				console.log(proxy.sjuLogin.getValue("realname"))
				proxy.sjuNav.navigateBack()
			})
			// proxy.sjuNav.navigateBack()
			 // proxy.sjuNav.redirectTo('/my/sureOrder?addressId='+item.id)
		}
	}
	onLoad((options) => {
		// queryAddressListFun()
		console.log(options)
		// let pages = getCurrentPages() // 获取栈实例
		// let page = pages[pages.length - 1] // 获取当前页面的数据，包含页面路由
		// let prevPage = pages[pages.length - 2] // 获取上个页面的数据，包含页面
		// console.log(page, prevPage.route)
	})
</script>

<style lang="scss" scoped>
	.myAddress {
		width: 100%;
		height: 100%;
		background-color: #FAFAFA;

		.addressbox {
			width: 100%;
			height: 150rpx;
			padding: 20rpx;
			margin-bottom: 15px;
			background: #fff;
			box-sizing: border-box;
			display: flex;
			flex-wrap: nowrap;
			justify-content: space-between;
			align-items: center;

			.leftbox {
				.address {
					display: flex;
					flex-wrap: nowrap;

					.site {
						color: #161616;
						font-size: 28rpx;
						font-weight: bold;
					}

					.default {
						padding: 5rpx 10rpx;
						background: #FDEBEB;
						color: #DF605E;
						font-size: 20rpx;
						margin: 0 10rpx;
						border-radius: 3px;
					}

					.home {
						padding: 5rpx 10rpx;
						background: #FDEBEB;
						color: #DF605E;
						font-size: 20rpx;
						margin: 0 10rpx;
						border-radius: 3px;
					}

				}

				.name {
					font-size: 24rpx;
					color: #8B8A91;
					height: 30px;
					line-height: 30px;
				}
			}
		}

		.addNew {
			width: 90%;
			height: 90rpx;
			line-height: 90rpx;
			font-size: 32rpx;
			border-radius: 20px;
			background: #3A8AFB;
			color: #fff;
			text-align: center;
			position: fixed;
			left: 5%;
			bottom: 5%
		}

	}
</style>